//
//  TLWaterfallFlowBannerCollectionViewCell.m
//  WXReader
//
//  Created by huaya on 2025/1/13.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLWaterfallFlowBannerCollectionViewCell.h"
#import "YJBannerView.h"
#import "TLMallWaterfallFlowCollectionViewCell.h"

#import "TLShortStoryViewController.h"

@interface TLWaterfallFlowBannerCollectionViewCell()<YJBannerViewDataSource, YJBannerViewDelegate>
@property (nonatomic, weak) YJBannerView *bannerView;
@end

@implementation TLWaterfallFlowBannerCollectionViewCell

- (void)setBannerList:(NSArray<TLMallBannerModel *> *)bannerList{
    if (bannerList) {
        _bannerList = bannerList;
        [self.bannerView reloadData];
//        [self.bannerView refresh];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

}
- (void)createSubviewsWith:(CGRect)rect {
    YJBannerView *bannerView = [YJBannerView bannerViewWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height) dataSource:self delegate:self emptyImage:kEmptyHoldImage placeholderImage:kEmptyHoldImage selectorString:NSStringFromSelector(@selector(setImageWithURL:placeholder:))];
    self.bannerView = bannerView;
    bannerView.repeatCount = 9999;
    bannerView.pageControlAliment = PageControlAlimentCenter;
    bannerView.autoDuration = 5.0f;
    bannerView.pageControlStyle = PageControlCustom;
    bannerView.pageControlDotSize = CGSizeMake(10, 5);
    bannerView.pageControlBottomMargin = 15;
    bannerView.pageControlHighlightColor = UIColor.themeColor;
    bannerView.pageControlNormalColor = [UIColor hex_DCDCDC];
    
    UIImage *highlight_image = [UIImage imageWithColor:[UIColor colorWithHexString:@"#9E6DFC"] size:CGSizeMake(6, 6)];
    
    highlight_image = [highlight_image imageByRoundCornerRadius:3.0];
    bannerView.customPageControlHighlightImage = highlight_image;
    UIImage *normal_image = [UIImage imageWithColor:[UIColor colorWithHexString:@"#D9D9D9"] size:CGSizeMake(6, 6)];
    normal_image = [normal_image imageByRoundCornerRadius:3.0];
    bannerView.customPageControlNormalImage = normal_image;  
    bannerView.backgroundColor = UIColor.clearColor;
    [self.contentView addSubview:bannerView];
    
}

- (NSArray *)bannerViewImages:(YJBannerView *)bannerView {
    
    return self.bannerList;
}

- (NSArray *)bannerViewRegistCustomCellClass:(YJBannerView *)bannerView
{
    return @[[TLMallWaterfallFlowCollectionViewCell class]];
}

- (Class)bannerView:(YJBannerView *)bannerView reuseIdentifierForIndex:(NSInteger)index
{
    return [TLMallWaterfallFlowCollectionViewCell class];
}

- (UICollectionViewCell *)bannerView:(YJBannerView *)bannerView customCell:(UICollectionViewCell *)customCell index:(NSInteger)index
{
    
    TLMallWaterfallFlowCollectionViewCell *cell = (TLMallWaterfallFlowCollectionViewCell *)customCell;
    
    cell.bannerModel = self.bannerList[index];
    
    
    return cell;
}

- (void)bannerView:(YJBannerView *)bannerView didSelectItemAtIndex:(NSInteger)index {
    
    TLMallBannerModel *bannerModel = self.bannerList[index];
    
    NSURL *url = [NSURL URLWithString:bannerModel.url];
    
    [TL_JumpManager jumpActionWithType:[url.path stringByReplacingOccurrencesOfString:@" " withString:@""] content:@"" otherParameter:[TL_UtilsHelper mk_dictionaryWithUrlQuery:url.query] completion:nil];
    
}
- (void)drawRect:(CGRect)rect{
    if (!_bannerView) {
        [self createSubviewsWith:rect];
        [self.bannerView reloadData];
    }else{
//        [self.bannerView reloadData];
    }
}
@end
